home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
keymap.exe
/
KEYMAP.C
< prev
next >
Wrap
Text File
|
1993-03-28
|
4KB
|
221 lines
#include <stdio.h>
#include <conio.h>
#define KEY_TABLE_SIZE 382
typedef struct KEYS
{
char name[32];
struct KEYS *next;
} KEY_NAME;
KEY_NAME *KeyTable[KEY_TABLE_SIZE];
char InFile[128];
char OutFile[128];
int EditSelectedKey( unsigned int k );
void DisplayNames( unsigned int k, FILE *optr );
void WriteKeyList( void );
void ReadKeyList( const char * infile );
main(int argc, char *argv[])
{
int j;
unsigned int c;
int rc;
if ( argc != 3 )
{
printf("\n\nUsage: keymap <infile> <outfile>\n\n");
return 1;
}
strcpy(InFile,argv[1]);
strcpy(OutFile,argv[2]);
for (j=0; j<KEY_TABLE_SIZE; j++)
KeyTable[j] = NULL;
ReadKeyList( InFile );
clrscr();
for ( ;; )
{
puts("\n\nPress a key: ");
c = getch();
if ( c == 0x1b )
{
WriteKeyList();
printf("!!!! DONE !!!!\n\n");
return 0;
}
puts("\n");
if ( ! c )
c = ( 126 + getch() );
if ( rc = EditSelectedKey( c ) )
return rc;
}
}
int EditSelectedKey( unsigned int k )
{
KEY_NAME *Temp;
KEY_NAME *CurKeyName;
char Name[32];
printf("\n\n[0x%0X] (%d decimal)\n",k,k);
DisplayNames( k, stdout );
CurKeyName = KeyTable[k];
if ( CurKeyName != NULL )
while ( CurKeyName->next != NULL )
CurKeyName = CurKeyName->next;
for (;;)
{
fputs("#define ",stdout);
gets( Name );
if ( ! strlen(Name) )
return 0;
Temp = (KEY_NAME *) calloc( 1, sizeof(KEY_NAME) );
if ( Temp == NULL )
{
printf("OUT OF MEMORY!!\n\n");
exit();
}
strcpy(Temp->name,Name);
Temp->next = NULL;
if (KeyTable[k] == NULL)
KeyTable[k] = Temp;
if ( CurKeyName == NULL )
CurKeyName = Temp;
else
{
CurKeyName->next = Temp;
CurKeyName = CurKeyName->next;
}
}
return 0;
}
void DisplayNames( unsigned int k, FILE *optr )
{
KEY_NAME *Temp;
Temp = KeyTable[k];
while ( Temp != NULL )
{
fprintf(optr,"#define %-31s 0x%0X\n",Temp->name,k);
Temp = Temp->next;
}
fputc('\n',optr);
}
void WriteKeyList( void )
{
int j;
KEY_NAME *Temp;
FILE *optr;
if ( (optr=fopen(OutFile,"w")) == NULL )
{
printf("Couldn't open output file '%s'!!!\n\n",OutFile);
return;
}
printf("\n\nWriting KeyMap to Disk.....");
for (j=0; j < KEY_TABLE_SIZE; j++)
{
if (KeyTable[j] != NULL)
{
DisplayNames(j,optr);
}
}
printf("Done!!\n\n");
fclose(optr);
}
void ReadKeyList( const char * infile )
{
FILE *iptr;
KEY_NAME *Temp;
KEY_NAME *CurKeyName;
int k;
char Control[8];
char Name[32];
char CodeString[256];
unsigned int Code;
if ( (iptr=fopen(infile,"r")) == NULL )
{
printf("Could not open Input File\n\n");
exit(2);
}
while ( ! feof(iptr) )
{
fscanf(iptr,"%7s %31s %x\n",Control,Name,&Code);
if (! strcmp("#define",Control) )
{
CurKeyName = KeyTable[Code];
if ( CurKeyName != NULL )
while ( CurKeyName->next != NULL )
CurKeyName = CurKeyName->next;
Temp = (KEY_NAME *) calloc( 1, sizeof(KEY_NAME) );
if ( Temp == NULL )
{
printf("OUT OF MEMORY!!\n\n");
exit();
}
strcpy(Temp->name,Name);
Temp->next = NULL;
if (KeyTable[Code] == NULL)
KeyTable[Code] = Temp;
if ( CurKeyName == NULL )
CurKeyName = Temp;
else
{
CurKeyName->next = Temp;
CurKeyName = CurKeyName->next;
}
} /**end if valid**/
}
fclose(iptr);
}